(function () {
    var core = ecui,
        dom = core.dom,
        util = core.util;
// https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx2054bed22814a315&redirect_uri=http%3A%2F%2Fu.mao-livehouse.com/maolive-app/index.html%23/venueOrder/search&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect

    ecui.util.extend(
        NS.ui,
        {
            SearchText: core.inherits(
                mao.ui.SearchText,
                {
                    oninput: function () {
                        // if (this.getValue()) {
                        //     dom.addClass(ecui.$('order_history_container'), 'ui-hide');
                        // } else {
                        //     dom.removeClass(ecui.$('order_history_container'), 'ui-hide');
                        // }
                        if (!dom.hasClass(ecui.$('order_history_container'), 'ui-hide')) {
                            dom.addClass(ecui.$('order_history_container'), 'ui-hide');
                        }
                        // ecui.esr.callRoute('historyList', true);
                        ecui.esr.callRoute('searchList', true);
                    },
                    onfocus: function () {
                        dom.removeClass(ecui.$('order_history_container'), 'ui-hide');
                        ecui.esr.callRoute('historyList', true);
                    },
                    onkeydown: function (event) {
                        if (event.which === 13) {
                            dom.addClass(ecui.$('order_history_container'), 'ui-hide');
                            ecui.esr.callRoute('searchList', true);
                            ecui.dispatchEvent(this, 'blur');
                            event.preventDefault();
                            event.stopPropagation();
                        }
                    }
                }
            ),
            History: core.inherits(
                ecui.ui.Control,
                function (el, options) {
                    ecui.ui.Control.call(this, el, options);
                    this._sValue = options.value;
                },
                {
                    onclick: function () {
                        document.forms.order_list_search_form.elements.reserveName.getControl().setValue(this._sValue);
                        dom.addClass(ecui.$('order_history_container'), 'ui-hide');
                        ecui.esr.callRoute('searchList', true);
                    }
                }
            ),
            ClearHistory: core.inherits(
                mao.ui.Submit,
                {
                    onclick: function () {
                        this.request(
                            'data@GET /erp-management/search/history/delete',
                            function () {
                                var data = ecui.esr.getData('data');
                                if (data instanceof Object) {
                                    var order_history_container = ecui.$('order_history_container');
                                    order_history_container.innerHTML = ecui.esr.getEngine().render('history_list', {});
                                    ecui.init(order_history_container);
                                }
                            },
                            function () {}
                        );
                    }
                }
            )
        }
    );
    core.esr.addRoute('search', {
        model: [],
        main: 'venueOrder_search',
        view: 'orderSearch',
        // frame: true,
        weight: 3,
        children: 'historyList',
        onbeforerender: function (context) {
            // console.log(context);
        },
        onafterrender: function () {
        },
        oncached: function () {
        }
    });
    core.esr.addRoute('historyList', {
        model: [
            'historys@GET /erp-management/search/history/list'
        ],
        main: 'order_history_container',
        view: 'history_list',
        frame: true,
        weight: 3,
        onbeforerender: function (context) {
            // console.log(context);
        },
        onafterrender: function () {
        },
        oncached: function () {
        }
    });
    core.esr.addRoute('searchList', {
        model: [],
        main: 'order_search_list_container',
        view: 'order_search_list',
        frame: true,
        weight: 3,
        onbeforerender: function (context) {
        },
        onafterrender: function () {
            ecui.dispatchEvent(ecui.get('order-search-list-view'), 'refresh');
        },
        oncached: function () {
        }
    });

}());